package cn.itrip.service.impl;

import cn.itrip.commons.RedisAPI;
import cn.itrip.dao.ItripUserLinkUserDao;
import cn.itrip.dto.Dto;
import cn.itrip.entity.ItripUser;
import cn.itrip.dao.ItripUserDao;
import cn.itrip.service.ItripUserService;
import cn.itrip.vo.UserLinkUserVO;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户表(ItripUser)表服务实现类
 *
 * @author makejava
 * @since 2022-04-14 22:35:00
 */
@Service("itripUserService")
public class ItripUserServiceImpl implements ItripUserService {
	@Resource
	private ItripUserDao itripUserDao;
	@Resource
	private ItripUserLinkUserDao itripUserLinkUserDao;
	@Resource
	private RedisAPI redisAPI;

	@Override
	public List<UserLinkUserVO> queryUserLinkUser(String token) throws Exception {
		ItripUser user =  JSONObject.parseObject(redisAPI.get(token),ItripUser.class);
		Assert.notNull(user,"会话过期，请先登录");
		return  itripUserLinkUserDao.queryByUserId(user.getId());
	}

	/**
	 * 通过ID查询单条数据
	 *
	 * @param id 主键
	 * @return 实例对象
	 */
	@Override
	public ItripUser queryById(Long id) {
		return this.itripUserDao.queryById(id);
	}

	/**
	 * 分页查询
	 *
	 * @param itripUser   筛选条件
	 * @param pageRequest 分页对象
	 * @return 查询结果
	 */
	@Override
	public Page<ItripUser> queryByPage(ItripUser itripUser, PageRequest pageRequest) {
		long total = this.itripUserDao.count(itripUser);
		return new PageImpl<>(this.itripUserDao.queryAllByLimit(itripUser, pageRequest), pageRequest, total);
	}

	/**
	 * 新增数据
	 *
	 * @param itripUser 实例对象
	 * @return 实例对象
	 */
	@Override
	public ItripUser insert(ItripUser itripUser) {
		this.itripUserDao.insert(itripUser);
		return itripUser;
	}

	/**
	 * 修改数据
	 *
	 * @param itripUser 实例对象
	 * @return 实例对象
	 */
	@Override
	public ItripUser update(ItripUser itripUser) {
		this.itripUserDao.update(itripUser);
		return this.queryById(itripUser.getId());
	}

	/**
	 * 通过主键删除数据
	 *
	 * @param id 主键
	 * @return 是否成功
	 */
	@Override
	public boolean deleteById(Long id) {
		return this.itripUserDao.deleteById(id) > 0;
	}
}
